package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
@TableBind(tableName="point_log",pkName = "log_id")
public class PointLog extends BasicModel<PointLog> {
    private static final long serialVersionUID = -2835090939430232763L;

    public static final PointLog dao = new PointLog();
    public static final String SELECT_ALL = "`log_id`,`user_id`,`point_rule_id`,`point_num`,`create_time`,`expire_time`,`type`,`creater`,`remark`,`last_update_time`";

    public List<PointLog> findUserLogByRule(long userid ,long ruleid){
        return find("select * from point_log where user_id = ? and point_rule_id = ?" ,userid,ruleid);
    }

    public PointLog sumLogActionTimes(long ruleId , Date start ,Date end , long userId){
        return findFirst("select sum(action_count) as action_sum from point_log " +
                "where point_rule_id = ? " +
                "and create_time >DATE_FORMAT(?,'%Y-%m-%d %T') and create_time < DATE_FORMAT(?,'%Y-%m-%d %T') " +
                "and user_id = ? ",ruleId,start,end,userId);

    }

    public List<PointLog> findPointLog(PointLog pointLog){
        List<Object> paras = new ArrayList<>();
        StringBuilder sqlExceptSelectBuffer = new StringBuilder("SELECT point_log.* FROM point_log ");

        sqlExceptSelectBuffer.append(" where 1=1 ");

        if (pointLog != null) {
            String[] attrNames = pointLog.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = pointLog.get(attrName);
                if (attrValue != null) {
                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND point_log.`create_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND point_log.`create_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
            sqlExceptSelectBuffer.append(" order by log_id desc");
        }
        return find(sqlExceptSelectBuffer.toString(),paras.toArray());
    }

    public Page<PointLog> paginate(Integer pageNumber, Integer pageSize, PointLog order) {
        List<Object> paras = new ArrayList<>();
        StringBuilder sqlExceptSelectBuffer = new StringBuilder("FROM point_log ");

        StringBuilder selectBuffer = new StringBuilder("SELECT point_log.* ");

        sqlExceptSelectBuffer.append(" where 1=1 ");
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        }else{
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (order != null) {
            String[] attrNames = order.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = order.get(attrName);
                if (attrValue != null) {
                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND point_log.`create_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND point_log.`create_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }

        Page<PointLog> orderPage;
        if (paras.size() > 0){
            orderPage = paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }else{
            orderPage = paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
        }
        return orderPage;
    }

    public PointLog setLogId (Long logId){
        this.set("log_id",logId);
        return this;
    }

    public Long getLogId (){
        return this. get("log_id");
    }

    public PointLog setUserId (Long userId){
        this.set("user_id",userId);
        return this;
    }

    public Long getUserId (){
        return this. get("user_id");
    }

    public PointLog setPointRuleId (Long pointRuleId){
        this.set("point_rule_id",pointRuleId);
        return this;
    }

    public Long getPointRuleId (){
        return this. get("point_rule_id");
    }

    public PointLog setPointNum (Integer pointNum){
        this.set("point_num",pointNum);
        return this;
    }

    public Integer getPointNum (){
        return this. get("point_num");
    }

    public PointLog setActionCount (Integer count){
        this.set("action_count",count);
        return this;
    }

    public Integer getActionCount (){
        return this. get("action_count");
    }

    public PointLog setStatus (Integer status){
        this.set("status",status);
        return this;
    }

    public Integer getStatus (){
        return this. get("status");
    }

    public PointLog setCreateTime (Date createTime){
        this.set("create_time",createTime);
        return this;
    }

    public Date getCreateTime (){
        return this.getTimestamp("create_time");
    }

    public PointLog setExpireTime (Date expireTime){
        this.set("expire_time",expireTime);
        return this;
    }

    public Date getExpireTime (){
        return this.getTimestamp("expire_time");
    }

    public PointLog setLastUpdateTime (Date lastUpdateTime){
        this.set("last_update_time",lastUpdateTime);
        return this;
    }

    public Date getLastUpdateTime (){
        return this.getTimestamp("last_update_time");
    }

    public PointLog setType (Integer type){
        this.set("type",type);
        return this;
    }

    public Integer getType (){
        return this. get("type");
    }

    public PointLog setCreater (Integer creater){
        this.set("creater",creater);
        return this;
    }

    public Integer getCreater (){
        return this. get("creater");
    }

    public PointLog setRemark (String remark){
        this.set("remark",remark);
        return this;
    }

    public String getRemark (){
        return this.getStr("remark");
    }

}
